---
title: "bigKRLS and KRLS convergence"
author: Pete Mohanty and Robert Shaffer
output: github_document
---

This code establishes the numeric convergence of bigKRLS and KRLS estimates and can be used to test the sensitivity of estimates to hyperparameter specification like `eigtrunc`. The results can be viewed [here](https://github.com/rdrr1990/bigKRLS/blob/master/examples/numeric_convergence.md).

```{r, message=FALSE, warning=FALSE, comment=""}
# all scripts in the replication materials assume bigKRLS 3.0.0 or higher
require(pacman)                   
p_load(bigKRLS, update = TRUE)
p_load(KRLS, session_info)

set.seed(2018)
N <- 500  
P <- 6
X <- matrix(rnorm(N*P), ncol=P)
X[,P] <- ifelse(X[,P] > 0.12345, 1, 0)
b <- runif(ncol(X))
y <- X %*% b + rnorm(nrow(X))

KRLS.out <- krls(X = X, y = y, print.level = 0, eigtrunc=0.01)
bigKRLS.out <- bigKRLS(y, X, instructions = FALSE, eigtrunc=0.01)

cor(KRLS.out$coeffs, bigKRLS.out$coeffs)    # should be 1

KRLS.out$avgderivatives
bigKRLS.out$avgderivatives

max(abs(bigKRLS.out$derivatives - KRLS.out$derivatives)) < 0.00000001

session_info()
```

